package com.handinfo.android.ui.window;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.CopyOnWriteArrayList;

import android.graphics.Bitmap;
import android.text.NoCopySpan.Concrete;
import android.util.MonthDisplayHelper;
import android.widget.Toast;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.graphics.DWFont;
import com.handinfo.android.core.net.GameProtocol;
import com.handinfo.android.core.object.Actor;
import com.handinfo.android.core.object.Animation;
import com.handinfo.android.game.Attribute;
import com.handinfo.android.game.Cell;
import com.handinfo.android.game.GameMessage;
import com.handinfo.android.game.Role;
import com.handinfo.android.game.item.Item;
import com.handinfo.android.game.item.ItemConstants;
import com.handinfo.android.game.item.ItemConsume;
import com.handinfo.android.game.item.ItemEquipment;
import com.handinfo.android.game.item.ItemGem;
import com.handinfo.android.ui.IUIWindows;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWDragListener;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.uicontrols.DWLongListener;
import com.handinfo.android.uicontrols.controls.DWAnimationBox;
import com.handinfo.android.uicontrols.controls.DWBackground;
import com.handinfo.android.uicontrols.controls.DWButton;
import com.handinfo.android.uicontrols.controls.DWControl;
import com.handinfo.android.uicontrols.controls.DWFrame;
import com.handinfo.android.uicontrols.controls.DWGrid;
import com.handinfo.android.uicontrols.controls.DWGrids;
import com.handinfo.android.uicontrols.controls.DWImageBox;
import com.handinfo.android.uicontrols.controls.DWListSubItem;
import com.handinfo.android.uicontrols.controls.DWListbox;
import com.handinfo.android.uicontrols.controls.DWMessageBox;
import com.handinfo.android.uicontrols.controls.DWTabControl;
import com.handinfo.android.uicontrols.controls.DWTabPage;
import com.handinfo.android.uicontrols.controls.DWTextbox;
import com.handinfo.android.uicontrols.controls.DWTitle;
import com.handinfo.android.utils.Tools;

/**
 * 
 * @author Xiaoyingying
 * 
 * 背包窗口
 * 
 */
public class UIBag implements IUIWindows
{
	public static final byte BAG_STATE_NORMAL = 0;// 普通状态
	public static final byte BAG_STATE_LIAOTIAN = 1;// 聊天状态背包
	public static final byte BAG_STATE_MAIL = 2;//邮件背包

	///////////////////自用////////////////////////
	private static final byte BAG_STATE_EQUIP = 3;//装备栏

	// 普通背包弹出菜单字符
	private final long FAILURETIME = 10;
	private final String UNINSTALL = "卸载";// 6006 ok
	private final String EQUIP = "装备";// 6006 ok
	private final String DESTROY = "丢弃";// 6003 ok
	private final String USE = "使用";// 6002 ok
	private final String RIDE = "骑上坐骑";// 6012 ok
	private final String RIDE_CANCEL = "取消坐骑";// 6012 ok
//	private final String HECHENG = "合成";
//	private final String JIEBANG = "解绑";

	//邮件
	private final String ADDGOODS = "添加";
	// 标题
	private final String TITLE = "背包";

//	// 数据
	private Cell[] m_bag_cell_equip;// 装备栏
	private Cell[] m_bag_cell_bags;
//	/**
//	 * 道具集合
//	 * 装备类：key为m_guid
//	 * 其他类：key为m_key
//	 */
	private int m_bag_open_num = 20;//开始背包的数量

	private int m_bag_state = 0; //背包状态

	private Boolean m_bag_isOpen = false; //背包开启

	private final int[] m_role_equip_index = { 0, 10, 11, 5, 6, 7, 1, 3, 4, 12, 2, 13, 8, 9 }; //武器在装备栏中索引

	private DWFrame m_bag_Fram; // 背包窗口
	private DWTitle m_bag_title; // 标题
	private DWTabControl m_bag_tabcontrol;// 标签页区域

	private String[] m_bag_pagTitle = { "全部", "装备", "消耗", "宝石", "其他" };
	private DWTabPage m_bag_tabpage[] = new DWTabPage[m_bag_pagTitle.length];

	private DWGrids m_bag_grids[] = new DWGrids[m_bag_pagTitle.length]; // 所有物品,武器,用品,其他

	public DWImageBox m_bag_closewindow;// 关闭窗口
	private DWImageBox m_bag_jinbi;
	private DWButton m_bag_neaten;// 整理背包
	private DWBackground m_bag_leftbackground;
	private DWBackground m_bag_rightbackground;;// 背包背景
	public DWButton m_bag_statsButton;
	private DWButton m_bag_bagButton;
	private DWButton m_bag_baitanButton;
	private DWButton m_bag_shizhuangButton;

	private Bitmap tabSelectImg;// 选项卡选中的图标
	private Bitmap tabUnSelectImg;// 先选卡未被选中的图标
	private Bitmap lockBoxImg; //背包锁
	private Bitmap closeWindow; // 关闭窗口图标
	private Bitmap neatenButtonImg;// 整理背包按钮图片
	private Bitmap anxia1;
	private Bitmap buttonImg; //按钮图标
	private Bitmap anxia2;
	private Bitmap medicament;//药瓶背景
	private Bitmap titleImg;
	private Bitmap frameBackground;
	private Bitmap jingbi;
	private Bitmap m_shizhuang1;//时装按钮
	private Bitmap m_shizhuang2;//时装按钮
	private DWImageBox m_shizhuang;//时装按钮
	private Bitmap[] cell_equip = new Bitmap[m_role_equip_index.length];

	public Actor m_star_Actor01 = null;//星星动画
	public Actor m_star_Actor02 = null;//星星动画
	public Actor m_star_Actor03 = null;//星星动画

	public void init()
	{
		lockBoxImg = UIWindows.createImage("/img/newui/beibaost_1.gnp");
		closeWindow = UIWindows.createImage("/img/newui/fanhuianniu_1.gnp");// 关闭窗口图标
		tabSelectImg = UIWindows.createImage("/img/newui/biaoqianye_2.gnp");
		tabUnSelectImg = UIWindows.createImage("/img/newui/biaoqianye_1.gnp");
		neatenButtonImg = UIWindows.createImage("/img/newui/anniu_1.gnp");
		buttonImg = UIWindows.createImage("/img/newui/anniu_2.gnp");
		anxia1 = UIWindows.createImage("/img/newui/anniu_1ax.gnp");
		anxia2 = UIWindows.createImage("/img/newui/anniu_2ax.gnp");
		medicament = UIWindows.createImage("/img/newui/xuepingdi_1.gnp");

		equipBoximg = UIWindows.createImage("/img/newui/wupinlan.gnp");// 背包物品背景
		titleImg = UIWindows.createImage("/img/newui/beibao_1.gnp");
		frameBackground = UIWindows.createImage("/img/newui/beibaobj_2.gnp");
		jingbi = UIWindows.createImage("/img/newui/jinbi_1.gnp");
		m_shizhuang1 = UIWindows.createImage("/img/newui/shizhuangguan_1.gnp");
		m_shizhuang2 = UIWindows.createImage("/img/newui/shizhuangkai_1.gnp");
		for (int i = 0; i < cell_equip.length; i++) {
			cell_equip[i] = UIWindows.createImage("/img/newui/cell_equip_" + i + ".gnp");
		}

		Animation star1 = UIWindows.createAnimation("/anim/star_3.anim");
		m_star_Actor01 = new Actor(star1);
		Animation star2 = UIWindows.createAnimation("/anim/star_2.anim");
		m_star_Actor02 = new Actor(star2);
		Animation star3 = UIWindows.createAnimation("/anim/star_1.anim");
		m_star_Actor03 = new Actor(star3);
	}

	/**
	 * 刷新背包
	 * @param cells
	 */

	public void updateWindow(Cell[] cell, byte cellType)
	{

		if (cell == null) {
			Tools.debugPrintln("刷新背包时，背包数据被付NULL");
			return;
		}

		if (cellType == Cell.TYPE_COLUMN_EQUIP)
		{
			UIWindows.getInstance().m_fix.setDurableMessage();
		}

		if (m_bag_isOpen) {
			switch (cellType) {
				case Cell.TYPE_COLUMN_ROLEBAG:
					m_bag_cell_bags = cell;
					m_bag_open_num = m_bag_cell_bags.length;
					int subType = m_bag_tabcontrol.getPageIndex();
					setMoney();
					addGrids(subType, getSubUIbag(m_bag_cell_bags, subType));
					break;
				case Cell.TYPE_COLUMN_EQUIP:
					m_bag_cell_equip = cell;
					setRoleBackgroundImg();
					break;
			}
		}
	}

	public Cell[] getSubUIbag(Cell[] cell, int subType)
	{
		Cell[] template = null;
		if (subType == Cell.INDEX_ROLEBAG_ALL) {
			template = new Cell[Role.COLUMN_MAX_SIZE[Cell.TYPE_COLUMN_ROLEBAG]];
			System.arraycopy(cell, 0, template, 0, cell.length);
		}
		else {
			int num = cell.length;
			if (cell != null && num > 0) {
				CopyOnWriteArrayList<Cell> array = new CopyOnWriteArrayList<Cell>();
				for (Cell c : cell) {
					if (c.m_item != null && c.m_item.m_type == subType) {
						array.add(c);
					}
				}
				int size = array.size();
				template = new Cell[size];
				for (int i = 0; i < size; i++) {
					Cell m = array.get(i);
					template[i] = m;
					Tools.debugPrintln("m.name=" + m.m_item.m_name);
				}
				array = null;
			}
		}

		return template;
	}

	public void open(byte index)
	{
		selectIndex = -1;
		m_bag_isOpen = true;
		m_bag_state = index;
		initFram();
		DWControlsManager.getInstance().addControl(m_bag_Fram);
	}

	public void close(byte index)
	{
		DWGameManager.getInstance().getSendMessage().unlockNetwork(GameMessage.锁_背包);

		if (item_Frame != null) {
			DWControlsManager.getInstance().removeControl(item_Frame);
			item_Frame = null;
		}
		DWControlsManager.getInstance().removeControl(m_bag_Fram);
		m_bag_Fram = null;
		m_bag_isOpen = false;
		UIWindows.getInstance().m_zhanli.open_other(DWGameManager.getInstance().m_role.m_zhanli);//战力相关
	}

	public void dispose()
	{
		m_bag_Fram = null;
	}

	/**
	 * 载入数据
	 */

	private void initData()
	{
		if (DWGameManager.getInstance().m_role != null) {
			m_RoleStats_role = DWGameManager.getInstance().m_role;
			m_bag_cell_equip = DWGameManager.getInstance().m_role.m_column_equip;// 装备栏
			m_bag_cell_bags = DWGameManager.getInstance().m_role.m_column_bag;// 背包栏
		}
	}

	public static int EQUIP_LENGTH = 12;

	private Bitmap equipBoximg; // 背包物品背景

	private Role m_RoleStats_role = null; // 显示人物的信息
	private DWAnimationBox m_RoleStats_Animation; // 显示人物动画 
	private DWGrid[] m_RoleStats_equipBox = new DWGrid[m_role_equip_index.length];// 装备栏
	public DWGrid m_shortcut_consume;
	private Bitmap bigImg;

	/**
	 * 创建人物装备界面
	 */
	public void creatRoleFram()
	{
		m_bag_leftbackground = new DWBackground((m_bag_Fram.getShowWidth() - 80) / 2, m_bag_Fram
				.getShowHeight()
				- 55 - neatenButtonImg.getHeight());
		m_bag_leftbackground.setNearAnchor(m_bag_Fram, Tools.RIGHT_VCENTER,
				Tools.HCENTER_VCENTER, -10, -neatenButtonImg.getHeight() / 4);
		m_bag_Fram.addControl(m_bag_leftbackground);

		if (m_RoleStats_role != null) {
			m_RoleStats_Animation = new DWAnimationBox(m_RoleStats_role);
			m_RoleStats_Animation.setNearAnchor(m_bag_leftbackground, Tools.HCENTER_VCENTER,
					Tools.HCENTER_VCENTER);
			m_RoleStats_Animation.addListener(new DWListener() {

				public void OnDoubleClick()
				{
				}

				public void OnClick()
				{
					close((byte) 0);
					UIWindows.getInstance().m_rolestats.open((byte) 0); //打开人物属性界面
				}
			});
			m_bag_Fram.addControl(m_RoleStats_Animation);
		}

		int moveX = (m_bag_leftbackground.getShowWidth() - equipBoximg.getWidth() * 5 - 20) / 4;

		int moveY = (m_bag_leftbackground.getShowHeight() - equipBoximg.getHeight() * 4 - 40) / 3;
		for (int i = 0; i < m_role_equip_index.length; i++) {
			m_RoleStats_equipBox[i] = new DWGrid(equipBoximg);
			switch (i) {
				case 0:
					m_RoleStats_equipBox[i].setNearAnchor(m_bag_leftbackground, Tools.LEFT_TOP, Tools.LEFT_TOP, 10, 20);
					break;
				case 5:
					m_RoleStats_equipBox[i].setNearAnchor(m_bag_leftbackground, Tools.LEFT_BOTTOM, Tools.LEFT_BOTTOM,
							10, -20);
					break;
				case 1:
				case 2:
				case 3:
				case 4:
				case 6:
				case 7:
				case 8:
				case 9:
					m_RoleStats_equipBox[i].setNearAnchor(m_RoleStats_equipBox[i - 1], Tools.LEFT_VCENTER,
							Tools.RIGHT_VCENTER, moveX, 0);
					break;
				case 10:
					m_RoleStats_equipBox[i].setNearAnchor(m_RoleStats_equipBox[0], Tools.HCENTER_TOP,
							Tools.HCENTER_BOTTOM, 0, moveY);
					break;
				case 11:
					m_RoleStats_equipBox[i].setNearAnchor(m_RoleStats_equipBox[5], Tools.HCENTER_BOTTOM,
							Tools.HCENTER_TOP, 0, -moveY);
					break;
				case 12:
					m_RoleStats_equipBox[i].setNearAnchor(m_RoleStats_equipBox[4], Tools.HCENTER_TOP,
							Tools.HCENTER_BOTTOM, 0, moveY);
					break;
				case 13:
					m_RoleStats_equipBox[i].setNearAnchor(m_RoleStats_equipBox[9], Tools.HCENTER_BOTTOM,
							Tools.HCENTER_TOP, 0, -moveY);
					break;
			}

			m_bag_Fram.addControl(m_RoleStats_equipBox[i]);
		}
		m_shortcut_consume = new DWGrid(medicament);
		m_shortcut_consume.setDarg(false);
		m_shortcut_consume.setNearAnchor(m_RoleStats_equipBox[11], Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER, 5, 0);
		m_shortcut_consume
				.addObject((Cell) DWGameManager.getInstance().m_role.getShortcutObject(Role.SHORTCUT_CONSUME));
		m_shortcut_consume.addDragListener(new DWDragListener() {
			public void OnDrag(DWControl control)
			{
				if (control instanceof DWGrids)
				{
					DWGrid grid = ((DWGrids) control).getTouchGrid();
					if (grid.m_data instanceof Cell)
					{
						Cell cell = (Cell) grid.m_data;
						Item item = (Item) cell.m_item;
						if (item != null && item instanceof ItemConsume && item.m_type == ItemConstants.道具类型_消耗
								&& item.m_subtype == ItemConstants.消耗_血蓝药) {
							Cell oldcell = (Cell) m_shortcut_consume.m_data;
							if (oldcell != null) {
								Item olditem = oldcell.m_item;
								if (olditem != null && olditem.m_key.equals(item.m_key)) {
									return;//两者相同就不用再设置
								}
							}
							Cell c = cell.Clone();
							c.m_item.m_num = DWGameManager.getInstance().m_role.getItemCountManager(
									Cell.TYPE_COLUMN_ROLEBAG, item.m_key);

							m_shortcut_consume.addObject(c);
							DWGameManager.getInstance().m_role.setShortcut(item.m_key, Role.SHORTCUT_CONSUME);
							c = null;
						}
						cell = null;
						item = null;
					}
				}
			}
		});

		m_shortcut_consume.addListener(new DWListener() {
			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				if (m_shortcut_consume.m_data != null)
				{
					Cell cell = (Cell) m_shortcut_consume.m_data;
					if (cell.m_item != null && cell.m_item.m_num > 0)
					{
						showItemMessage((Cell) m_shortcut_consume.m_data, BAG_STATE_NORMAL);
					}
				}
			}
		});
		m_bag_Fram.addControl(m_shortcut_consume);

		m_shizhuang = new DWImageBox(m_shizhuang1);
		if (m_shizhuang_state == 1) {
			m_shizhuang.setImage(m_shizhuang2);
		}
		else {
			m_shizhuang.setImage(m_shizhuang1);
		}
		m_shizhuang.setNearAnchor(m_shortcut_consume, Tools.HCENTER_BOTTOM, Tools.HCENTER_TOP, 0, -45);
		m_shizhuang.addListener(new DWListener() {
			public void OnDoubleClick()
			{
				// TODO Auto-generated method stub
			}

			public void OnClick()
			{
				// TODO Auto-generated method stub
				byte state;
				if (m_shizhuang_state == 0) {
					state = 1;
				}
				else {
					state = 0;
				}
				sendshizhuang(state);
			}
		});
		m_bag_Fram.addControl(m_shizhuang);

		m_bag_statsButton = new DWButton("属性", neatenButtonImg);
		m_bag_statsButton.setDownImage(anxia1);
		m_bag_statsButton.setNearAnchor(m_bag_leftbackground, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 10, 5);
		m_bag_statsButton.addListener(new DWListener() {
			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				close((byte) 0);
				UIWindows.getInstance().m_rolestats.open((byte) 0);
			}
		});
		m_bag_Fram.addControl(m_bag_statsButton);

		m_bag_bagButton = new DWButton("仓库", neatenButtonImg);
		m_bag_bagButton.addListener(new DWListener() {
			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				close((byte) 0);
				DWGameManager.getInstance().getSendMessage().sendScenePathfinding("lxd_cangku_1");
			}
		});
		m_bag_bagButton.setDownImage(anxia1);
		m_bag_bagButton.setNearAnchor(m_bag_leftbackground, Tools.HCENTER_TOP, Tools.HCENTER_BOTTOM, 0, 5);
		m_bag_Fram.addControl(m_bag_bagButton);
		Tools.debugPrintln("人物信息");

//		
		m_bag_baitanButton = new DWButton("摆摊", neatenButtonImg);
		m_bag_baitanButton.addListener(new DWListener() {
			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				Tools.debugPrintln("进入摆摊寻路界面");
				close((byte) 0);

				UIWindows.getInstance().m_baitan.sendBaitanResult();
			}
		});
		m_bag_baitanButton.setDownImage(anxia1);
		m_bag_baitanButton.setNearAnchor(m_bag_leftbackground, Tools.RIGHT_TOP, Tools.RIGHT_BOTTOM, -10, 5);
		m_bag_Fram.addControl(m_bag_baitanButton);

		if (DWGameManager.getInstance().m_role != null && m_bag_cell_equip != null) {
			for (int i = 0; i < m_role_equip_index.length; i++) {
				if (m_bag_cell_equip.length > i && m_bag_cell_equip[i] != null)
					setRoleImg(m_bag_cell_equip[i]);
			}
		}
	}

	private void setLeft(int num)
	{
		if (selectIndex > -1)
		{
			m_RoleStats_equipBox[selectIndex].setSelect(false);
			selectIndex = num;
			m_RoleStats_equipBox[num].setSelect(true);
		}
		for (DWGrids grids : m_bag_grids) {
			grids.getTouchGrid().setSelect(false);
		}
	}

	private void setRight()
	{
		if (selectIndex > -1)
		{
			m_RoleStats_equipBox[selectIndex].setSelect(false);
			selectIndex = -1;
		}
	}

	private DWButton m_bag_enchase;//镶嵌强化
	private DWTextbox m_bag_moneyNum;//金币数

	/**
	 * 创建背包标签页
	 */
	private void creatBagPage()
	{
		if (m_bag_state == BAG_STATE_NORMAL) {
			m_bag_rightbackground = new DWBackground((m_bag_Fram.getShowWidth() - 80) / 2, m_bag_Fram
					.getShowHeight()
					- 60 - tabUnSelectImg.getHeight() - 5 - neatenButtonImg.getHeight());
			m_bag_rightbackground.setNearAnchor(m_bag_leftbackground, Tools.LEFT_BOTTOM, Tools.RIGHT_BOTTOM, 10, 0);
		}

		else {
			m_bag_rightbackground = new DWBackground(m_bag_Fram.getShowWidth() - 30, m_bag_Fram
					.getShowHeight()
					- 60 - tabUnSelectImg.getHeight() - 5 - neatenButtonImg.getHeight());
			m_bag_rightbackground.setNearAnchor(m_bag_Fram, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, 0,
					tabUnSelectImg.getHeight() / 2);
		}

		m_bag_Fram.addControl(m_bag_rightbackground);

		addTabControl();
		addMoney();

		if (m_bag_state != BAG_STATE_NORMAL)
			return;

		m_bag_neaten = new DWButton("整理", neatenButtonImg);
		m_bag_neaten.setDownImage(anxia1);
		m_bag_neaten.addListener(new DWListener() {
			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				m_bag_neaten.setFailureTime(FAILURETIME);
				actionTrim();
			}
		});
		m_bag_neaten.setNearAnchor(m_bag_rightbackground, Tools.RIGHT_TOP,
				Tools.RIGHT_BOTTOM, 0, 5);
		m_bag_Fram.addControl(m_bag_neaten);

		m_bag_enchase = new DWButton("强化", neatenButtonImg);
		m_bag_enchase.setDownImage(anxia1);
		m_bag_enchase.setNearAnchor(m_bag_rightbackground, Tools.HCENTER_TOP, Tools.HCENTER_BOTTOM, 0, 5);
		m_bag_enchase.addListener(new DWListener() {
			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				close((byte) 0);
				UIWindows.getInstance().m_tiangong.open((byte) 0);
			}
		});

		if (DWGameManager.getInstance().m_role.m_lv < 10)
			m_bag_enchase.setFailureTime(Integer.MAX_VALUE);

		m_bag_Fram.addControl(m_bag_enchase);
	}

	/**
	 * 刷新金钱数量
	 */
	public void setMoney()
	{
		if (m_bag_Fram == null || (!DWControlsManager.getInstance().contains(m_bag_Fram)))
			return;

		addMoney();
	}

	/**
	 * 设置金钱数量
	 */
	private void addMoney()
	{
		if (m_bag_moneyNum == null)
		{
			m_bag_moneyNum = new DWTextbox("");
			m_bag_jinbi = new DWImageBox(jingbi);
		}

		if (!m_bag_Fram.contains(m_bag_moneyNum))
		{
			m_bag_Fram.addControl(m_bag_moneyNum);
			m_bag_Fram.addControl(m_bag_jinbi);
		}

		m_bag_moneyNum.setText("<mark c=FFBB11>" + splitMoney(m_RoleStats_role.m_gold) + "</mark>");

		if (m_bag_state != BAG_STATE_NORMAL)
		{
			m_bag_moneyNum.setNearAnchor(m_bag_Fram, Tools.LEFT_BOTTOM, Tools.LEFT_BOTTOM, 20, -10);
			m_bag_jinbi.setNearAnchor(m_bag_moneyNum, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER, 5, 0);
		}
		else
		{
			m_bag_jinbi.setNearAnchor(m_bag_rightbackground, Tools.RIGHT_TOP, Tools.HCENTER_BOTTOM,
					-(neatenButtonImg.getWidth() / 2 + 10), 10);
			m_bag_moneyNum.setNearAnchor(m_bag_jinbi, Tools.RIGHT_VCENTER, Tools.LEFT_VCENTER, -5, 0);
		}

	}

	/**
	 * 数字分割
	 * @param money
	 * @return
	 */
	public static String splitMoney(long money)
	{
		String moneyStr = String.valueOf(money);

		if (moneyStr.length() <= 3)
			return moneyStr;

		CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<String>();

		for (int i = moneyStr.length();; i = i - 3)
		{
			int start = i - 3;
			if (start < 0)
				start = 0;

			String str = moneyStr.substring(start, i);
			list.add(str);
			if (start == 0)
				break;
		}

		String msg = "";
		for (int i = 0; i < list.size(); i++)
		{
			msg = list.get(i) + (i == 0 ? "" : ",") + msg;
		}

		list = null;
		return msg;
	}

	/**
	 * 添加背包格子
	 */
	private void addTabControl()
	{
		m_bag_tabcontrol = new DWTabControl(tabUnSelectImg, tabSelectImg, m_bag_rightbackground.getShowWidth(),
				m_bag_rightbackground.getShowHeight() + tabUnSelectImg.getHeight() - 10);
		m_bag_tabcontrol.setNearAnchor(m_bag_rightbackground, Tools.LEFT_TOP, Tools.LEFT_TOP, 0, -tabUnSelectImg
				.getHeight());
		for (int i = 0; i < m_bag_pagTitle.length; i++) {

			m_bag_tabpage[i] = new DWTabPage(); // 建立标签页
			m_bag_tabpage[i].setName(m_bag_pagTitle[i]);// 设置标签页标	
			m_bag_grids[i] = new DWGrids(equipBoximg, Role.COLUMN_MAX_SIZE[Cell.TYPE_COLUMN_ROLEBAG], 5, 4);

			int gridsX = (m_bag_tabcontrol.getShowWidth() - equipBoximg.getWidth() * 5 - 30) / 5;
			int gridsY = (m_bag_tabcontrol.getShowHeight() - equipBoximg.getWidth() * 4 - 10 - tabUnSelectImg
					.getHeight()) / 4;

			m_bag_grids[i].setInterspace(gridsX, gridsY);
			m_bag_grids[i].setNearAnchor(m_bag_tabpage[i], Tools.LEFT_TOP, Tools.LEFT_TOP, 10, 10); // 设置控件在标签页中的位置

			m_bag_tabpage[i].addControls(m_bag_grids[i]); // 把控件添加到标签页中
		}
		updateWindow(m_bag_cell_bags, Cell.TYPE_COLUMN_ROLEBAG);
		m_bag_tabcontrol.addTabPages(m_bag_tabpage); // 把标签页添加到DWTabControl中

		m_bag_tabcontrol.addListener(new DWListener() {
			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				Tools.debugPrintln("刷新背包界面");
				updateWindow(m_bag_cell_bags, Cell.TYPE_COLUMN_ROLEBAG);
			}
		});
		m_bag_Fram.addControl(m_bag_tabcontrol);
	}

	/**
	 * 在标签页中添加背包格
	 */

	private void addGrids(final int num, final Cell[] cell)
	{

		// 在标签页中添加东西
		if (DWGameManager.getInstance().m_role != null) {
			for (int i = 0; i < m_bag_open_num; i++) {
				m_bag_grids[num].m_grids.get(i).m_data = null;
			}

			m_bag_grids[num].setGridsImage(equipBoximg, 0, m_bag_open_num - 1);
			m_bag_grids[num].setGridsImage(lockBoxImg, m_bag_open_num,
					Role.COLUMN_MAX_SIZE[Cell.TYPE_COLUMN_ROLEBAG] - 1);
			setBagGridObject(cell, m_bag_grids[num]);
		}
	}

	/**
	 * 背包拖动监听
	 */

	private void dragListener(DWGrids grids, DWControl control, Cell[] cell)
	{
		DWGrids fromGrids = grids;
		if (control instanceof DWGrids) //移动操作
		{
			DWGrids endGrids = (DWGrids) control;

			if (endGrids.getDragGird().m_data != null) {
				Cell fromcell = cell[fromGrids.getTouchIndex()];
				Cell endcell = cell[endGrids.getDragIndex()];

				if (endGrids.equals(fromGrids)) {
					if (endGrids.getDragIndex() != fromGrids.getTouchIndex()) //移动操作
					{
						if (endcell.m_item == null) {
							actionMove(fromcell, endcell); //发送移动请求
						}
						else {
							DWGameManager.getInstance().showToast("请放到空的背包格中", Toast.LENGTH_SHORT);
						}
					}
				}
			}
			else
				DWGameManager.getInstance().showToast("无法移动", Toast.LENGTH_SHORT);

		}
		else if (control instanceof DWGrid)//卸载操作
		{

			DWGrid endGrid = (DWGrid) control;
			if (fromGrids.getDragGird().m_data != null) {
				Cell selectID = (Cell) fromGrids.getDragGird().m_data;
				if (((Cell) fromGrids.getDragGird().m_data).m_item == null) {
					actionMove((Cell) endGrid.m_data, selectID);
				}
				else if (((Cell) fromGrids.getDragGird().m_data).m_item != null
						&& selectID.m_item.m_type == Cell.INDEX_ROLEBAG_EQUIP) {
					actionMove((Cell) endGrid.m_data, selectID);
				}
				else {
					DWGameManager.getInstance().showToast("请选择正确的物品", Toast.LENGTH_SHORT);
				}
			}
			else {
				DWGameManager.getInstance().showToast("无法移动", Toast.LENGTH_SHORT);
			}
		}
	}

	/**
	 * 设置人物装备图片
	 */

	private int selectIndex = -1;

	private void setRoleImg(final Cell cell)
	{
		final int num = m_role_equip_index[cell.m_index];

		m_RoleStats_equipBox[num].addObject(cell);
		if (cell.m_item == null)
			m_RoleStats_equipBox[num].setImage(cell_equip[cell.m_index]);

		m_RoleStats_equipBox[num].addListener(new DWListener() {

			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				setLeft(num);
				if (selectIndex > -1)
				{
					m_RoleStats_equipBox[selectIndex].setSelect(false);
				}
				selectIndex = num;
				m_RoleStats_equipBox[num].setSelect(true);
				if (cell.m_item != null)
					showItemMessage(cell, BAG_STATE_EQUIP);
			}
		});
		m_RoleStats_equipBox[num].addDragListener(new DWDragListener() {

			public void OnDrag(DWControl control)
			{
				Tools.debugPrintln("裝備");

				if (control instanceof DWGrids)
				{
					DWGrids endGrids = (DWGrids) control;
					Cell endcell = (Cell) m_RoleStats_equipBox[num].m_data;
					Cell fromcell = (Cell) endGrids.getTouchGrid().m_data;
					if (fromcell.m_item instanceof ItemEquipment) {
						ItemEquipment equip = (ItemEquipment) fromcell.m_item;
						if (!equip.m_isLimit) {
							actionMove(fromcell, endcell); //发送移动请求
							return;
						}
					}
					DWGameManager.getInstance().showToast("该物品不能装备", Toast.LENGTH_SHORT);
				}
			}
		});
	}

	private void setRoleBackgroundImg()
	{
		if (DWGameManager.getInstance().m_role != null) {
			for (int i = 0; i < m_role_equip_index.length; i++) {
				Cell cell = (Cell) m_RoleStats_equipBox[i].m_data;
				m_RoleStats_equipBox[i].setImage(cell_equip[cell.m_index]);
				if (cell.m_item != null)
					m_RoleStats_equipBox[i].setImage(equipBoximg);
			}
		}
	}

	/**
	 * 设置背包图片
	 */

	private void setBagGridObject(final Cell[] cell, final DWGrids grids)
	{
		if (cell != null && grids != null) {
			for (int i = 0; i < cell.length; i++) {
				grids.m_grids.get(i).addObject(cell[i]);

				if (cell[i] != null) {
					if (cell[i].m_item == null) {
						grids.m_grids.get(i).setDarg(true);
					}
					else
					{
						grids.m_grids.get(i).setDarg(false);
					}

				}

				if (m_bag_state == BAG_STATE_MAIL) {
					if (cell[i] != null) {
						if (cell[i].m_item != null) {
							if (cell[i].m_item.m_bindingState == ItemConstants.绑定状态_是
									|| (!cell[i].m_item.isOperate(ItemConstants.可交易)))
							{
								grids.m_grids.get(i).setDarg(true);
							}
						}
					}
				}
			}

			switch (m_bag_state) {
				case BAG_STATE_NORMAL: //普通背包的监听
					grids.addListener(new DWListener() {
						public void OnDoubleClick()
						{
						}

						public void OnClick()
						{
							setRight();
							int index = grids.getTouchIndex();
							if (index >= m_bag_open_num)
							{
								showBuyWindow(index);
								return;
							}
							if (grids.m_grids.get(index).m_data != null
									&& ((Cell) grids.m_grids.get(index).m_data).m_item != null)
							{
								showItemMessage((Cell) grids.m_grids.get(index).m_data, BAG_STATE_NORMAL);
							}
						}
					});

					grids.addDragListener(new DWDragListener() {

						public void OnDrag(DWControl control)
						{
							if (DWGameManager.getInstance().getSendMessage().isNetLocked(GameMessage.锁_背包))
								return;
							dragListener(grids, control, cell);
						}
					});
					grids.addLongListener(new DWLongListener() {

						public void OnLong(boolean isDown)
						{
							if (DWGameManager.getInstance().getSendMessage().isNetLocked(GameMessage.锁_背包))
								return;

							if (isDown)
							{
								setRight();
								if (grids.getTouchGrid().m_data != null
										&& ((Cell) grids.getTouchGrid().m_data).m_item.m_type == ItemConstants.道具类型_装备)
								{
									int num = m_role_equip_index[((Cell) grids.getTouchGrid().m_data).m_item.m_subtype];
									if (selectIndex != -1)
									{
										m_RoleStats_equipBox[selectIndex].setSelect(false);
										selectIndex = -1;
									}
									m_RoleStats_equipBox[num].setSelect(true);
								}
							}
							else
							{
								int index = ((Cell) grids.getTouchGrid().m_data).m_item.m_subtype;
								if (index > -1 && index < m_role_equip_index.length)
								{
									int num = m_role_equip_index[index];
									m_RoleStats_equipBox[num].setSelect(false);
								}
							}

						}
					});
					break;
				case BAG_STATE_LIAOTIAN: //聊天背包的监听
					grids.addListener(new DWListener() {
						public void OnDoubleClick()
						{
							Cell cell = (Cell) (grids.m_grids.get(grids.getTouchIndex()).m_data);
							String str = UIWindows.getInstance().m_liaotian.m_inputbox.getText() + "["
									+ cell.m_item.m_name + "]";
							UIWindows.getInstance().m_liaotian.m_inputbox.setText(str);
						}

						public void OnClick()
						{
							int index = grids.getTouchIndex();

							if (grids.m_grids.get(index).m_data != null
									&& ((Cell) grids.m_grids.get(index).m_data).m_item != null)
							{
								showItemMessage((Cell) grids.m_grids.get(index).m_data, BAG_STATE_LIAOTIAN);
							}

						}
					});
					break;
				case BAG_STATE_MAIL: //邮件背包
					grids.addListener(new DWListener() {
						public void OnDoubleClick()
						{
							Cell cell = (Cell) (grids.m_grids.get(grids.getTouchIndex()).m_data);
							UIWindows.getInstance().m_mail.m_xq_item_grid.m_data = cell;
						}

						public void OnClick()
						{
							int index = grids.getTouchIndex();

							if (grids.m_grids.get(index).m_data != null
									&& ((Cell) grids.m_grids.get(index).m_data).m_item != null)
							{
								showItemMessage((Cell) grids.m_grids.get(index).m_data, BAG_STATE_MAIL);
							}

						}
					});
					grids.addDragListener(new DWDragListener() {

						public void OnDrag(DWControl control)
						{
							if (control instanceof DWGrid)
							{
								DWGrid grid = (DWGrid) control;
								grid.m_data = null;
							}
						}
					});
					break;
			}

		}
	}

	/**
	 * 初始化界面
	 */
	private void initFram()
	{
		initData(); //获取数据

		if (DWControlsManager.getInstance().contains(m_bag_Fram))
		{
			DWControlsManager.getInstance().removeControl(m_bag_Fram);
			m_bag_Fram = null;
		}

		switch (m_bag_state) {
			case BAG_STATE_NORMAL:
				m_bag_Fram = new DWFrame(DWFrame.FRAME_TYPE_FULL);
				break;
			case BAG_STATE_LIAOTIAN:
			case BAG_STATE_MAIL:
				m_bag_Fram = new DWFrame(DWFrame.FRAME_TYPE_HALF_RIGHT);
				break;
		}

		m_bag_Fram.setClickClose(false);

		m_bag_title = new DWTitle(TITLE, m_bag_Fram);
		m_bag_title.setBackground(titleImg, false);
		m_bag_Fram.addControl(m_bag_title);

		m_bag_closewindow = new DWImageBox(closeWindow);
		m_bag_closewindow.setNearAnchor(m_bag_title, Tools.RIGHT_VCENTER,
				Tools.RIGHT_VCENTER);
		m_bag_closewindow.setTouchZoomIn(UIFixWindow.TOUCHZOOM, UIFixWindow.TOUCHZOOM);
		m_bag_closewindow.addListener(new DWListener() {

			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				close((byte) 0);
			}
		});
		m_bag_Fram.addControl(m_bag_closewindow);

		switch (m_bag_state) {
			case BAG_STATE_NORMAL:
				creatRoleFram();
			case BAG_STATE_LIAOTIAN:
			case BAG_STATE_MAIL:
				creatBagPage();
				break;
		}
		Tools.debugPrintln("背包创建");
	}

	/**
	 * 购买背包界面
	 */
	public void showBuyWindow(final int openNum)
	{
		final DWMessageBox buyWindow = new DWMessageBox("提示", "确定花费"
				+ (DWGameManager.getInstance().m_role.m_bag_unitPrice * (openNum + 1 - m_bag_open_num)) + "元宝开启"
				+ (openNum + 1 - m_bag_open_num) + "个格子");
		buyWindow.addListener(new DWListener() {

			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				DWControlsManager.getInstance().removeControl(buyWindow);
				if (buyWindow.getResult() == DWMessageBox.SOFTKEY_OK) {
					actionKuorong(openNum);
				}
			}
		});
		DWControlsManager.getInstance().addControl(buyWindow);
	}

	/**
	 * 获取物品菜单选项
	 */
	public String[] getButtonMenuList(int menuType, Cell cell)
	{
		Tools.debugPrintln("menuType:" + menuType);

		String[] buttonMenus = null;

		switch (menuType) {
			case BAG_STATE_EQUIP:// 装备格子
				buttonMenus = new String[] { UNINSTALL };
				break;
			case BAG_STATE_NORMAL:// 背包格子
				switch (cell.m_item.m_type) {
					case ItemConstants.道具类型_装备:
						if (cell.m_item.m_bindingType == 0) {
//							buttonMenus = new String[] { DESTROY, JIEBANG, EQUIP };
							buttonMenus = new String[] { DESTROY, EQUIP };
						}
						else {
							buttonMenus = new String[] { DESTROY, EQUIP };
						}
						break;
					case ItemConstants.道具类型_消耗:
//						buttonMenus = new String[] { DESTROY, JIEBANG, USE };
						if (cell.m_item.m_subtype == ItemConstants.消耗_血蓝药)
						{
							Cell consymeCell = (Cell) DWGameManager.getInstance().m_role
									.getShortcutObject(Role.SHORTCUT_CONSUME);
							if (consymeCell == null
									|| (cell.m_item.m_key != consymeCell.m_item.m_key))
								buttonMenus = new String[] { DESTROY, EQUIP, USE };
							else
								buttonMenus = new String[] { DESTROY, USE };
						}
						else
							buttonMenus = new String[] { DESTROY, USE };
						break;
					case ItemConstants.道具类型_宝石:
//						buttonMenus = new String[] { DESTROY, JIEBANG };
						buttonMenus = new String[] { DESTROY };
						break;
					case ItemConstants.道具类型_其他:
						switch (cell.m_item.m_subtype) {
							case ItemConstants.其他_坐骑:
								if (DWGameManager.getInstance().m_role.m_isride) {
//									buttonMenus = new String[] { DESTROY, JIEBANG, RIDE_CANCEL };
									buttonMenus = new String[] { DESTROY, RIDE_CANCEL };
								}
								else {
//									buttonMenus = new String[] { DESTROY, JIEBANG, RIDE };
									buttonMenus = new String[] { DESTROY, RIDE };
								}
								break;
							default:
//								buttonMenus = new String[] { DESTROY, JIEBANG };
								buttonMenus = new String[] { DESTROY };
								break;
						}
						break;
					default:
//						buttonMenus = new String[] { DESTROY, JIEBANG };
						buttonMenus = new String[] { DESTROY };
						break;
				}
				break;
			case BAG_STATE_MAIL:
				if (cell.m_item.m_bindingState == ItemConstants.绑定状态_否 && cell.m_item.isOperate(ItemConstants.可交易))
					buttonMenus = new String[] { ADDGOODS };
				break;
			case BAG_STATE_LIAOTIAN:
				buttonMenus = new String[] { ADDGOODS };
				break;
		}
		return buttonMenus;
	}

	/////////////////////////////////////////////////////////////
	DWFrame bag_item_Frame;
	DWBackground item_background;
	DWListbox item_listbox;
	DWListSubItem item_listsubitem;
	DWButton[] m_item_button;
	String[] buttonMenu;
	DWImageBox bag_item_close;
	int showWidth = ((DWGameManager.Screen_Width * 5) >> 4) + 20;

	/**
	 * 显示物品信息窗口
	 * @param cell 显示的格子
	 * @param menuType 显示类型，装备，背包，邮件，和其他（只显示物品信息，无操作）
	 */
	private void showItemMessage(final Cell cell, int menuType)
	{
		m_item_button = null;
		buttonMenu = null;
		if (DWControlsManager.getInstance().contains(bag_item_Frame))
			DWControlsManager.getInstance().removeControl(bag_item_Frame);

		Item item = cell.m_item;
		int anchorx = 0;

		Tools.debugPrintln("显示宽度：" + showWidth);
		Tools.debugPrintln("道具类型：" + item.m_type);
		if (menuType == BAG_STATE_NORMAL && item.m_type == ItemConstants.道具类型_装备
				&& m_bag_cell_equip[item.m_subtype].m_item != null) {
			bag_item_Frame = new DWFrame(((DWGameManager.Screen_Width * 5) >> 3) + 20,
					DWGameManager.Screen_Height * 5 / 9);
			Tools.debugPrintln("………………………………………………装备对比…………………………………………");
		}
		else {
			bag_item_Frame = new DWFrame(((DWGameManager.Screen_Width * 5) >> 4) + 20,
					DWGameManager.Screen_Height * 5 / 9);
		}
		bag_item_Frame.setClickClose(false);
		bag_item_Frame.setBackgroundColor(Tools.ALPHA);

		Tools.debugPrintln("信息界面宽：" + bag_item_Frame.getShowWidth() + "信息界面高：" + bag_item_Frame.getShowHeight());
		item_background = new DWBackground(frameBackground, bag_item_Frame.getShowWidth(), bag_item_Frame
				.getShowHeight());
		item_background.setNearAnchor(bag_item_Frame, Tools.LEFT_TOP, Tools.LEFT_TOP);
		bag_item_Frame.addControl(item_background);

		bag_item_close = new DWImageBox(closeWindow);
		bag_item_close.addListener(new DWListener() {
			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				DWControlsManager.getInstance().removeControl(bag_item_Frame);
			}
		});
		bag_item_close.setTouchZoomIn(UIFixWindow.TOUCHZOOM, UIFixWindow.TOUCHZOOM);
		bag_item_close.setNearAnchor(bag_item_Frame, Tools.HCENTER_VCENTER, Tools.RIGHT_TOP);
		bag_item_Frame.addControl(bag_item_close);

		item_listbox = new DWListbox(bag_item_Frame.getShowWidth() - 20, bag_item_Frame.getShowHeight() - 30
				- buttonImg.getHeight());
		item_listbox.setShowBackgroundRect(false);
		item_listbox.setNearAnchor(bag_item_Frame, Tools.HCENTER_TOP, Tools.HCENTER_TOP, 0, 20);
		bag_item_Frame.addControl(item_listbox);

		item_listsubitem = new DWListSubItem();
		//item_listsubitem中添加东西
		if (menuType == BAG_STATE_NORMAL && item.m_type == ItemConstants.道具类型_装备
				&& m_bag_cell_equip[item.m_subtype].m_item != null) {
			getItemWindow(item_listsubitem, 0, 0, showWidth, m_bag_cell_equip[item.m_subtype].m_item);
			DWTextbox message = new DWTextbox("<mark c=FFBB11>已装备</mark>");
			message.setNearAnchor(item_listsubitem, Tools.LEFT_TOP, Tools.LEFT_TOP, 20, 0);
			item_listsubitem.addControls(message);
			anchorx = bag_item_Frame.getShowWidth() >> 1;
		}
		getItemWindow(item_listsubitem, anchorx, 0, showWidth, cell.m_item);
		item_listsubitem.refreshControl();
		item_listbox.addSubItem(item_listsubitem);

		buttonMenu = getButtonMenuList(menuType, cell);

		if (buttonMenu != null && (!buttonMenu.equals(""))) {
			for (int i = 0; i < buttonMenu.length; i++) {
				Tools.debugPrintln("buttonMenu" + i + buttonMenu[i]);
			}
			m_item_button = new DWButton[buttonMenu.length];
			int item_button_x = (showWidth - buttonImg.getWidth() * buttonMenu.length)
					/ (buttonMenu.length * 2);

			if (buttonMenu != null && buttonMenu.length > 0) {
				for (int i = buttonMenu.length - 1; i >= 0; i--) {
					final int buttonID = i;
					m_item_button[i] = new DWButton(buttonMenu[i], buttonImg);
					m_item_button[i].setDownImage(anxia2);
					if (i == buttonMenu.length - 1) {
						m_item_button[i]
								.setNearAnchor(item_listbox, Tools.RIGHT_TOP, Tools.RIGHT_BOTTOM, -item_button_x, 5);
					}
					else
					{
						m_item_button[i].setNearAnchor(m_item_button[i + 1], Tools.RIGHT_VCENTER, Tools.LEFT_VCENTER,
								-(item_button_x << 1), 0);
					}

					m_item_button[i].addListener(new DWListener() {

						public void OnDoubleClick()
						{
						}

						public void OnClick()
						{
							if (cell.m_item != null) {
								DWControlsManager.getInstance().removeControl(bag_item_Frame);
								bag_item_Frame = null;
								ItemKeyListener(cell, buttonID, buttonMenu);
							}
						}
					});

					bag_item_Frame.addControl(m_item_button[i]);
				}
			}
		}
		DWControlsManager.getInstance().addControl(bag_item_Frame);
	}

	private DWFrame item_netFrame;
	private DWTextbox item_textbox;
	private String item_key;

	/**
	 * 显示网络返回物品
	 * @param item
	 */
	public void showNetItemMessage(Item item)
	{
		if (item_netFrame != null && DWControlsManager.getInstance().contains(item_netFrame))
		{
//			if(item_key==item.m_key)
//			{
			DWControlsManager.getInstance().removeControl(item_netFrame);
			showItemMessage(item);
//			}				
		}
	}

	/**
	 * 显示loading~
	 */
	public void openNetItemMessage(String key)
	{
		item_key = key; //记录最后一次点击的item
		if (item_netFrame == null || !DWControlsManager.getInstance().contains(item_netFrame))
		{
			item_netFrame = new DWFrame((DWGameManager.Screen_Width * 3) >> 3, DWGameManager.Screen_Height * 7 / 9);
			item_netFrame.setBackgroundColor(Tools.ALPHA);
			item_netFrame.setClickClose(false);

			DWBackground item_background = new DWBackground(frameBackground, item_netFrame.getShowWidth(),
					item_netFrame.getShowHeight());
			item_background.setNearAnchor(item_netFrame, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER);
			item_netFrame.addControl(item_background);

			DWImageBox close = new DWImageBox(closeWindow);
			close.addListener(new DWListener() {
				public void OnDoubleClick()
				{
				}

				public void OnClick()
				{
					DWControlsManager.getInstance().removeControl(item_netFrame);
				}
			});
			close.setNearAnchor(item_netFrame, Tools.HCENTER_VCENTER, Tools.RIGHT_TOP);
			close.setTouchZoomIn(UIFixWindow.TOUCHZOOM, UIFixWindow.TOUCHZOOM);
			item_netFrame.addControl(close);

			item_textbox = new DWTextbox("载入中...");
			item_textbox.setNearAnchor(item_netFrame, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER);
			item_netFrame.addControl(item_textbox);

			starTime = 0;
			DWControlsManager.getInstance().addControl(item_netFrame);
		}
	}

	public DWFrame item_Frame;

	/**
	 * 公共的显示物品界面
	 * @param item
	 * @param moneyType 出售价格的类型
	 */
	public void showItemMessage(Item item, byte moneyType)
	{

		if (item == null)
			return;

		if (DWControlsManager.getInstance().contains(item_Frame))
			DWControlsManager.getInstance().removeControl(item_Frame);

		item_Frame = new DWFrame((DWGameManager.Screen_Width * 3) >> 3,
				DWGameManager.Screen_Height * 7 / 9);
		item_Frame.setBackgroundColor(Tools.ALPHA);
		item_Frame.setClickClose(false);

		item_background = new DWBackground(frameBackground, item_Frame.getShowWidth(), item_Frame.getShowHeight());
		item_background.setNearAnchor(item_Frame, Tools.LEFT_TOP, Tools.LEFT_TOP);
		item_Frame.addControl(item_background);

		DWImageBox close = new DWImageBox(closeWindow);
		close.addListener(new DWListener() {
			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				DWControlsManager.getInstance().removeControl(item_Frame);
			}
		});
		close.setNearAnchor(item_Frame, Tools.HCENTER_VCENTER, Tools.RIGHT_TOP);
		close.setTouchZoomIn(UIFixWindow.TOUCHZOOM, UIFixWindow.TOUCHZOOM);
		item_Frame.addControl(close);

		item_listbox = new DWListbox(item_Frame.getShowWidth() - 40, item_Frame.getShowHeight() - 70);
		item_listbox.setShowBackgroundRect(false);
		item_listbox.setNearAnchor(item_Frame, Tools.HCENTER_TOP, Tools.HCENTER_TOP, 0, 20);
		item_Frame.addControl(item_listbox);

		item_listsubitem = new DWListSubItem();
		getItemWindow(item_listsubitem, 0, 0, showWidth, item, moneyType);
		item_listsubitem.refreshControl();
		item_listbox.addSubItem(item_listsubitem);
		DWControlsManager.getInstance().addControl(item_Frame);
	}

	/**
	 * 公共的显示物品界面
	 * @param item
	 */
	public void showItemMessage(Item item)
	{
		showItemMessage(item, Role.CURRENCY_TYPE_GOLD);
	}

	/**
	 * 在一个DWListSubItem中显示物品的详细信息
	 * @param subitem 绘制的DWListSubItem
	 * @param showWidth 绘制模块显示的宽度
	 * @param showcell 需要绘制的item
	 */
	public void getItemWindow(DWListSubItem subitem, int showWidth, Item showcell)
	{
		getItemWindow(subitem, 0, 0, showWidth, showcell);
	}

	/**
	 * 绘制一块信息
	 * @param subitem 绘制的DWListSubItem
	 * @param x 绘制模块在DWListSubItem中起始的X坐标
	 * @param y 绘制模块在DWListSubItem中起始的y坐标
	 * @param showWidth 绘制模块显示的宽度
	 * @param showcell 需要绘制的item
	 */
	public void getItemWindow(DWListSubItem subitem, int x, int y, int showWidth, Item showcell)
	{
		getItemWindow(subitem, x, y, showWidth, showcell, Role.CURRENCY_TYPE_GOLD);
	}

	/**
	 * 绘制一块信息
	 * @param subitem 绘制的DWListSubItem
	 * @param x 绘制模块在DWListSubItem中起始的X坐标
	 * @param y 绘制模块在DWListSubItem中起始的y坐标
	 * @param showWidth 绘制模块显示的宽度
	 * @param showcell 需要绘制的item
	 * @param moneyType 需要显示的价格类型
	 */
	public void getItemWindow(DWListSubItem subitem, int x, int y, int showWidth, Item showcell, byte moneyType)
	{
		DWGrid item_role_img = new DWGrid(equipBoximg);
		item_role_img.setDarg(true);
		item_role_img.addObject(showcell);
		item_role_img.setNearAnchor(subitem, Tools.LEFT_TOP, Tools.LEFT_TOP, x + 10, y + 20);
		subitem.addControls(item_role_img);

		addItemMessage(subitem, showWidth - 30, item_role_img, showcell, moneyType);
	}

	public static final String[][] 类型名称 = {
			{ "武器", "头部", "上衣", "腰带", "裤子", "鞋子", "称号", "项链", "手腕", "戒指", "时装", "法宝", "护符", "坐骑", "预留1", "预留2" },
			{ "", "血蓝药", "状态药", "增强药", "传送券", "触发任务卷", "礼包", "喇叭", "宝箱1", "商城道具", "宝箱2", "宠物蛋", "", "" },
			{ "", "宝石" },
			{ "", "任务", "特殊功能", "钥匙" }
	};
	public static final String[] 装备_绑定类型 = { "不绑定", "装备后绑定", "拾取后绑定", "使用后绑定" };
	public static final String[] 装备_基础属性 = { "物理攻击", "法术攻击", "物理防御", "法术防御" };

	DWControl lastControl;

	/**
	 * 获得物品信息字符串
	 * @param item
	 * @return
	 */
	public void addItemMessage(DWListSubItem subitem, int showWidth, DWGrid grid, Item item, byte moneyType)
	{
		Tools.debugPrintln("" + showWidth);
		lastControl = null;
		//装备名称
		String name = "<mark c=" + getNameColor(item.m_quality) + ">" + item.m_name + "</mark>";

		if (item.m_type == ItemConstants.道具类型_装备) {
			if (((ItemEquipment) item).m_strengthenLevel > 0)
				name += "<mark c=FFBB11>("
						+ ((ItemEquipment) item).getProductOrder(((ItemEquipment) item).m_strengthenLevel) + ")</mark>";
		}

		DWTextbox item_equip_name = new DWTextbox(name, showWidth - grid.getShowWidth() - 20);
		item_equip_name.setNearAnchor(grid, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER, 20, 0);
		subitem.addControls(item_equip_name);

		//TODO
		if (item.m_type == ItemConstants.道具类型_装备) {
			DWAnimationBox[] m_animBox_star = new DWAnimationBox[((ItemEquipment) item).m_attrLv];
			if (((ItemEquipment) item).m_attrLv > 0 && ((ItemEquipment) item).m_attrLv <= 3) {
				for (int i = 0; i < ((ItemEquipment) item).m_attrLv; i++) {
					m_animBox_star[i] = new DWAnimationBox(m_star_Actor01);
					m_animBox_star[i].setShowWideHigh(20, 0);
					m_animBox_star[0].setNearAnchor(grid, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 20);
					if (i > 0) {
						m_animBox_star[i].setNearAnchor(m_animBox_star[i - 1], Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER,
								5, 0);
					}
					subitem.addControls(m_animBox_star[i]);
					lastControl = m_animBox_star[0];
				}
			}
			else if (((ItemEquipment) item).m_attrLv > 3 && ((ItemEquipment) item).m_attrLv <= 6) {
				for (int i = 0; i < 3; i++) {
					m_animBox_star[i] = new DWAnimationBox(m_star_Actor01);
					m_animBox_star[i].setShowWideHigh(20, 0);
					m_animBox_star[0].setNearAnchor(grid, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 20);
					if (i > 0) {
						m_animBox_star[i].setNearAnchor(m_animBox_star[i - 1], Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER,
								5, 0);
					}
					subitem.addControls(m_animBox_star[i]);
					lastControl = m_animBox_star[0];
				}
				for (int i = 3; i < ((ItemEquipment) item).m_attrLv; i++) {
					m_animBox_star[i] = new DWAnimationBox(m_star_Actor02);
					m_animBox_star[i].setShowWideHigh(20, 0);
					m_animBox_star[i].setNearAnchor(m_animBox_star[i - 1], Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER,
							5, 0);
					subitem.addControls(m_animBox_star[i]);
				}
			}
			else if (((ItemEquipment) item).m_attrLv > 6 && ((ItemEquipment) item).m_attrLv <= 9) {
				for (int i = 0; i < 3; i++) {
					m_animBox_star[i] = new DWAnimationBox(m_star_Actor01);
					m_animBox_star[i].setShowWideHigh(20, 0);
					m_animBox_star[0].setNearAnchor(grid, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 20);
					if (i > 0) {
						m_animBox_star[i].setNearAnchor(m_animBox_star[i - 1], Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER,
								5, 0);
					}
					subitem.addControls(m_animBox_star[i]);
					lastControl = m_animBox_star[0];
				}
				for (int i = 3; i < 6; i++) {
					m_animBox_star[i] = new DWAnimationBox(m_star_Actor02);
					m_animBox_star[i].setShowWideHigh(20, 0);
					m_animBox_star[i].setNearAnchor(m_animBox_star[i - 1], Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER,
							5, 0);
					subitem.addControls(m_animBox_star[i]);
				}
				for (int i = 6; i < ((ItemEquipment) item).m_attrLv; i++) {
					m_animBox_star[i] = new DWAnimationBox(m_star_Actor03);
					m_animBox_star[i].setShowWideHigh(20, 0);
					m_animBox_star[i].setNearAnchor(m_animBox_star[i - 1], Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER,
							5, 0);
					subitem.addControls(m_animBox_star[i]);
				}
			}
		}

		//装备类别
		Tools.debugPrintln("item.m_type=" + item.m_type + "item.m_subtype=" + item.m_subtype);
		String type = "<mark c=FFBB11>类别：</mark>";
		if (item.m_type > 0 && 类型名称.length > (item.m_type - 1) && 类型名称[item.m_type - 1].length > item.m_subtype)
		{
			type += 类型名称[item.m_type - 1][item.m_subtype];
		}
		if (item.m_bindingState == ItemConstants.绑定状态_是) {
			type += "  已绑定";
		}
		else if (item.m_bindingState == ItemConstants.绑定状态_否) {
			type += "  " + 装备_绑定类型[item.m_bindingType];
		}

		DWTextbox item_equip_type = new DWTextbox(type, showWidth);
		if (item.m_type == ItemConstants.道具类型_装备 && ((ItemEquipment) item).m_attrLv > 0) {
			item_equip_type.setNearAnchor(lastControl, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 5);
		}
		else {
			item_equip_type.setNearAnchor(grid, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 20);
		}
		subitem.addControls(item_equip_type);

		lastControl = item_equip_type;

		//装备评分
		if (item.m_type == ItemConstants.道具类型_装备 && ((ItemEquipment) item).m_gs > 0) {

			DWTextbox item_equip_gs = new DWTextbox("<mark c=FFBB11>装备评分:</mark>" + ((ItemEquipment) item).m_gs,
					showWidth);
			item_equip_gs.setNearAnchor(lastControl, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 5);
			subitem.addControls(item_equip_gs);

			lastControl = item_equip_gs;
		}

		//使用限制
		String limit = "<mark c=FFBB11>使用限制:</mark>";

		String lev = "";
		int m_lv = DWGameManager.getInstance().m_role.m_lv;
		if (item.m_minLevelLimit > 0) {
			lev = item.m_minLevelLimit + "级以上使用";
			if (m_lv < item.m_minLevelLimit)
				lev = "<mark c=FF3300>" + lev + "</mark>";
		}
		if (item.m_maxLevelLimit > 0) {
			lev = item.m_maxLevelLimit + "级以下使用";
			if (m_lv > item.m_maxLevelLimit)
				lev = "<mark c=FF3300>" + lev + "</mark>";
		}

		limit += lev;

		String str = "   " + Role.ROLE_VOCATION_LIST[item.m_jobLimit];
		if (item.m_jobLimit != DWGameManager.getInstance().m_role.m_vocation) {
			str = "<mark c=FF3300>" + str + "</mark>";
		}

		if (item.m_jobLimit > 0)
			limit += str;

		if (!limit.trim().equals("<mark c=FFBB11>使用限制:</mark>"))
		{
			DWTextbox item_equip_Limit = new DWTextbox(limit, showWidth);
			item_equip_Limit.setNearAnchor(lastControl, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 5);
			subitem.addControls(item_equip_Limit);

			lastControl = item_equip_Limit;
		}

		//耐久值
		if (item.m_type == ItemConstants.道具类型_装备 && ((ItemEquipment) item).m_maxDurable > 0) {
			String durable = "<mark c=FFBB11>耐久值：</mark>";

			durable += (((ItemEquipment) item).m_nowDurable == 0 ? "<mark c=FF3300>" : "")
					+ ((ItemEquipment) item).m_nowDurable + "/" + ((ItemEquipment) item).m_maxDurable
					+ (((ItemEquipment) item).m_nowDurable == 0 ? "</mark>" : "");
			DWTextbox item_equip_Durable = new DWTextbox(durable, showWidth);
			item_equip_Durable.setNearAnchor(lastControl, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 5);
			subitem.addControls(item_equip_Durable);
			lastControl = item_equip_Durable;
		}

		//基础属性
		if (item.m_type == ItemConstants.道具类型_装备) {
			String baseDate = "";
			for (int i = 0; i < 4; i++) {
				if (((ItemEquipment) item).m_attrValue[i + Attribute.PHY_ATTACK] != 0) {
					if (((ItemEquipment) item).m_attrAddValus_forge[i + Attribute.PHY_ATTACK] != 0) {
						baseDate += "<mark c=FFBB11>" + Attribute.ATTRIBUTE_NAME[i + Attribute.PHY_ATTACK] + "：</mark>";
						baseDate += "<mark c=FFBB11>" + ((ItemEquipment) item).m_attrValue[i + Attribute.PHY_ATTACK]
								+ "</mark>";

						if (((ItemEquipment) item).m_attrAddValue[i + Attribute.PHY_ATTACK] > 0
								|| ((ItemEquipment) item).m_attrAddValus_forge[i + Attribute.PHY_ATTACK] > 0) {
							baseDate += "  <mark c=FFBB11>+"
									+ (((ItemEquipment) item).m_attrAddValue[i + Attribute.PHY_ATTACK]
									+
									+((ItemEquipment) item).m_attrAddValus_forge[i + Attribute.PHY_ATTACK]) + "</mark>";
						}
						else if (((ItemEquipment) item).m_attrAddValue[i + Attribute.PHY_ATTACK] < 0
								&& ((ItemEquipment) item).m_attrAddValus_forge[i + Attribute.PHY_ATTACK] < 0)
							baseDate += "  <mark c=FF3300>"
									+ ((ItemEquipment) item).m_attrAddValue[i + Attribute.PHY_ATTACK]
									+ "+ "
									+ ((ItemEquipment) item).m_attrAddValus_forge[i + Attribute.PHY_ATTACK]
									+ "</mark>";
						if (i != 3)
							baseDate += "`";
					}
					else {
						baseDate += "<mark c=FFBB11>" + Attribute.ATTRIBUTE_NAME[i + Attribute.PHY_ATTACK] + "：</mark>";
						baseDate += ((ItemEquipment) item).m_attrValue[i + Attribute.PHY_ATTACK];

						if (((ItemEquipment) item).m_attrAddValue[i + Attribute.PHY_ATTACK] > 0) {
							baseDate += "    <mark c=FFBB11>+"
									+ ((ItemEquipment) item).m_attrAddValue[i + Attribute.PHY_ATTACK] + "</mark>";
						}
						else if (((ItemEquipment) item).m_attrAddValue[i + Attribute.PHY_ATTACK] < 0)
							baseDate += "    <mark c=FF3300>"
									+ ((ItemEquipment) item).m_attrAddValue[i + Attribute.PHY_ATTACK] + "</mark>";
						if (i != 3)
							baseDate += "`";
					}
				}
			}
			if (!baseDate.equals("")) {
				DWTextbox item_equip_baseDate = new DWTextbox(baseDate, showWidth);
				item_equip_baseDate.setNearAnchor(lastControl, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 5);
				subitem.addControls(item_equip_baseDate);

				lastControl = item_equip_baseDate;
			}

			//镶嵌宝石宝石

			if (((ItemEquipment) item).m_equipGemsSize > 0) {
				DWImageBox gemsImg[] = new DWImageBox[((ItemEquipment) item).m_equipGemsSize];

				int gemsShowWidth = (int) (getShowWidth(((ItemEquipment) item).getGemDatas(0), ((ItemEquipment) item)
						.getGemDatas(2)) + 20);

				for (int i = 0; i < gemsImg.length; i++) {
					gemsImg[i] = new DWImageBox(((ItemEquipment) item).m_gemSamllIcon[i]);
					DWTextbox gemsMsg = new DWTextbox("<mark c=FFBB11> " + ((ItemEquipment) item).getGemDatas(i)
							+ "</mark>");
					if (i % 2 == 0) {
						gemsImg[i].setNearAnchor(lastControl, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 5);
						lastControl = gemsImg[i];
					}
					else {
						gemsImg[i].setNearAnchor(gemsImg[i - 1], Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER,
								gemsShowWidth, 0);
					}
					gemsMsg.setNearAnchor(gemsImg[i], Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER, 5, 0);
					subitem.addControls(gemsImg[i]);
					subitem.addControls(gemsMsg);
				}
			}
		}

		//随机的附加属性
		if (item.m_type == ItemConstants.道具类型_装备) {
			StringBuilder attrValue = new StringBuilder();
			for (int i = 0; i < ((ItemEquipment) item).m_attrValue.length; i++) {
				if (i > Attribute.SPEED && i < Attribute.HIT)
					continue;

				if (((ItemEquipment) item).m_attrValue[i] > 0) {
					if (((ItemEquipment) item).m_attrAddValus_forge[i] > 0) {
						attrValue.append("<mark c=00FF00>" + Attribute.ATTRIBUTE_NAME[i] + ":+"
								+ ((ItemEquipment) item).m_attrValue[i] + "</mark>"
								+ "<mark c=FFBB11>  +"
								+ ((ItemEquipment) item).m_attrAddValus_forge[i] + "</mark>`");
					}
					else {
						attrValue.append("<mark c=00FF00>" + Attribute.ATTRIBUTE_NAME[i] + ":+"
								+ ((ItemEquipment) item).m_attrValue[i] + "</mark>`");
					}
				}
				else if (((ItemEquipment) item).m_attrValue[i] < 0) {
					attrValue.append("<mark c=999999>" + Attribute.ATTRIBUTE_NAME[i] + ":"
							+ ((ItemEquipment) item).m_attrValue[i] + "</mark>`");
				}
			}

			if (attrValue != null && attrValue.length() > 0) {
				DWTextbox item_equip_attrValue = new DWTextbox(attrValue.toString(), showWidth);
				item_equip_attrValue.setNearAnchor(lastControl, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 10);
				subitem.addControls(item_equip_attrValue);

				lastControl = item_equip_attrValue;
			}
		}
		else if (item.m_type == ItemConstants.道具类型_宝石) {
			String attrValue = "<mark c=00FF00>" + Attribute.ATTRIBUTE_NAME[((ItemGem) item).m_attrindex] + ": +"
					+ ((ItemGem) item).m_attrvalue + "</mark>";
			DWTextbox item_equip_attrValue = new DWTextbox(attrValue, showWidth);
			item_equip_attrValue.setNearAnchor(lastControl, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 10);
			subitem.addControls(item_equip_attrValue);
			lastControl = item_equip_attrValue;
		}

		//套装

		//描述
		if (item.m_descl != null && !(item.m_descl.equals(""))) {
			String prodyctOrder = "";
			prodyctOrder = "<mark c=FFFFFF>" + item.m_descl + "</mark>";

			DWTextbox item_equip_prodyctOrder = new DWTextbox(prodyctOrder, showWidth);
			item_equip_prodyctOrder.setNearAnchor(lastControl, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 5);
			subitem.addControls(item_equip_prodyctOrder);
			lastControl = item_equip_prodyctOrder;
		}

		//寄宿精魂

		//解绑次数
		if (item.m_maxUnbindingTimes > 0)
		{
			String bindingTimes = "<mark c=FFBB11>解绑次数：</mark>"
					+ (item.m_maxUnbindingTimes - item.m_usedUnbindingTimes)
					+ "/" + item.m_maxUnbindingTimes;
			DWTextbox item_equip_bindingTimes = new DWTextbox(bindingTimes, showWidth);
			item_equip_bindingTimes.setNearAnchor(lastControl, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 5);
			subitem.addControls(item_equip_bindingTimes);
			lastControl = item_equip_bindingTimes;
		}

		//限制时间
		if (item.m_lifecycle > 0)
		{
			Calendar c = Calendar.getInstance();
			c.setTime(new Date((item.m_birthday + item.m_lifecycle) * 1000));

			DWTextbox item_equip_lifecycle = new DWTextbox("<mark c=FFBB11>到期时间：</mark>"
					+ getLifeTime(c), showWidth);

			item_equip_lifecycle.setNearAnchor(lastControl, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 5);
			subitem.addControls(item_equip_lifecycle);
			lastControl = item_equip_lifecycle;
		}

		//出售价格
		String str_goldPrice = "";
		if (item.isOperate(ItemConstants.可出售))
		{
			str_goldPrice = "<mark c=FFBB11>出售价格：</mark>" + UINpcShopWindow.getItemPrice(item, moneyType);
		}
		else
		{
			str_goldPrice += "<mark c=FFBB11>不可出售</mark>";
		}
		DWTextbox item_equip_goldPrice = new DWTextbox(str_goldPrice, showWidth);
		item_equip_goldPrice.setNearAnchor(lastControl, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 5);
		subitem.addControls(item_equip_goldPrice);
		lastControl = item_equip_goldPrice;

		//
		DWTextbox item_equip_end = new DWTextbox("<mark c=FFFFFF></mark>", showWidth);
		item_equip_end.setNearAnchor(lastControl, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 5);
		subitem.addControls(item_equip_end);

	}

	/**
	 * 
	 * @return
	 */
	private float getShowWidth(String str1, String str2)
	{
		float[] widthArray = { DWFont.getDefaultFont().stringWidth(str1), DWFont.getDefaultFont().stringWidth(str2) };
		return widthArray[0] > widthArray[1] ? widthArray[0] : widthArray[1];
	}

	/**
	 * 获得到期时间
	 * @param c
	 * @return
	 */
	public static String getLifeTime(Calendar c)
	{
		String str = "";
		String timeStr[] = { "年", "月", "日", "时" };

		str = c.get(Calendar.YEAR) + "年" + (c.get(Calendar.MONTH) + 1) + "月" + c.get(Calendar.DAY_OF_MONTH) + "日"
				+ c.get(Calendar.HOUR_OF_DAY) + "时" + c.get(Calendar.MINUTE) + "分";
		return str;
	}

	/**
	 * 将秒转换为x天x秒x分
	 * @param time
	 * @return
	 */
	public static final byte DAYTYPE = 0;
	public static final byte HOURTYPE = 1;
	public static final byte MINUTETYPE = 2;

	public static String getTime(long time, byte showType)
	{
		int[] nums = { 86400, 3600, 60 };
		String[] timeName = { "天", "时", "分" };
		String timeStr = "";

		long flag = time;
		for (int i = 0; i < 3; i++) {
			if (i >= showType)
			{
				int num = 0;
				num = (int) flag / nums[i];
				flag = time % nums[i];
				timeStr += num + timeName[i];
			}
		}
		timeStr += flag + "秒";
		return timeStr;
	}

	/**
	 * 获取item名字的颜色
	 * @param item
	 * @return
	 */
	public static String getNameColor(int num)
	{
		String color = "";
		if (num >= 0) {
			switch (num) {
				case ItemConstants.稀有度_灰:
					color = "999999";
					break;
				case ItemConstants.稀有度_白:
					color = "ffffff";
					break;
				case ItemConstants.稀有度_蓝:
					color = "00BBFF";
					break;
				case ItemConstants.稀有度_绿:
					color = "55FF00";
					break;
				case ItemConstants.稀有度_紫:
					color = "9900FF";
					break;
				case ItemConstants.稀有度_橙:
					color = "FFBB11";
					break;
				case ItemConstants.稀有度_红:
					color = "FF3300";
					break;
			}
		}
		return color;
	}

	public static int getQualityColor(int num)
	{
		int color = 0xFF999999;
		if (num >= 0) {
			switch (num) {
				case ItemConstants.稀有度_灰:
					color = 0xFF999999;
					break;
				case ItemConstants.稀有度_白:
					color = 0xFFFFFFFF;
					break;
				case ItemConstants.稀有度_蓝:
					color = 0xFF00BBFF;
					break;
				case ItemConstants.稀有度_绿:
					color = 0xFF55FF00;
					break;
				case ItemConstants.稀有度_紫:
					color = 0xFF9900FF;
					break;
				case ItemConstants.稀有度_橙:
					color = 0xFFFFBB11;
					break;
				case ItemConstants.稀有度_红:
					color = 0xFFFF3300;
					break;
			}
		}
		return color;
	}

	/**
	 * 获取物品菜单监听
	 * 
	 * @param item
	 * 选中物品
	 * @param menuID
	 * 选中菜单选项
	 * @param menus
	 * 菜单选项
	 */
	public void ItemKeyListener(final Cell cell, final int menuID,
			final String[] menus)
	{
		String temp = menus[menuID];
		if (temp == EQUIP) {
			actionEquip(cell);
		}
		else if (temp == UNINSTALL) {
			actionUninstall(cell);
		}
		//		}
		else if (temp == DESTROY) {
			actionDestroy(cell);
		}
		else if (temp == USE) {
			actionUse(cell);
		}
		else if (temp == RIDE) {
//			 actionRide((ActorItemRide) item);
		}
		else if (temp == RIDE_CANCEL) {
			// actionUnRide((ActorItemRide) item);
		}
		//		else if (temp == JIEBANG) {
////			 actionJiebang(item);
//		}
		else if (temp == ADDGOODS) {
			switch (m_bag_state) {
				case BAG_STATE_MAIL:
					if (UIWindows.getInstance().m_mail.m_send_item_grid != null)
					{
						UIWindows.getInstance().m_mail.m_item_cell = cell.Clone();
						UIWindows.getInstance().m_mail.Choose();
					}
					break;
				case BAG_STATE_LIAOTIAN:
					if (UIWindows.getInstance().m_liaotian.m_inputbox != null)
					{
						String str = UIWindows.getInstance().m_liaotian.m_inputbox.getText();
						if (str == null)
							str = "";

						str += "<mark t=i c=" + getNameColor(cell.m_item.m_quality) + " i=" + cell.m_item.m_key + ">["
								+ cell.m_item.m_name + "]</mark>";
						UIWindows.getInstance().m_liaotian.m_inputbox.setText(str);
					}
					break;
			}

		}
	}

	/**
	 * 整理
	 */
	private void actionTrim()
	{
		Tools.debugPrintln("整理背包 -> actionTrim");
		DWGameManager.getInstance().getSendMessage().sendArrangeMentColumnCommand(Cell.TYPE_COLUMN_ROLEBAG);
	}

	/**
	 * 扩容
	 */
	private void actionKuorong(int index)
	{
		Tools.debugPrintln("扩容道具 -> actionKuorong");
		DWGameManager.getInstance().getSendMessage().sendOperaterCellMenuCommand(GameProtocol.操作菜单_扩充栏,
				Cell.TYPE_COLUMN_ROLEBAG, index);
	}

	/**
	 * 使用
	 * 
	 * @param item
	 */
	private void actionUse(Cell cell)
	{
		Tools.debugPrintln("使用道具 -> actionUse");
//		Item item = cell.m_item;
//		if (item.m_type == ItemConstants.道具类型_消耗) {
//			ItemConsume consume = (ItemConsume) item;
//			if (DWGameManager.getInstance().m_role.isCding(consume.m_cd_type)) {
//				DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "道具Cd时间还未结束");
//				return;
//			}
//		}

		if (cell.m_item.m_type == ItemConstants.道具类型_消耗 && cell.m_item.m_subtype == ItemConstants.消耗_喇叭)
		{
			UIWindows.getInstance().m_tyfon.m_id = cell.m_item.m_guid;
			UIWindows.getInstance().m_tyfon.open((byte) 0);
		}
		else if (cell.m_item.m_type == ItemConstants.道具类型_消耗 && cell.m_item.m_subtype == ItemConstants.消耗_锻造符) {
			UIWindows.getInstance().m_tiangong.m_baohufu_guid = cell.m_item.m_guid;
			UIWindows.getInstance().m_tiangong.open((byte) 1);
		}
		else
		{
			if (cell != null) {
				cell.doShortcutAction();
			}
//			DWGameManager.getInstance().getSendMessage().sendOperaterCellMenuCommand(GameProtocol.操作菜单_使用,
//					(byte) cell.m_column_type, cell.m_index);
		}

	}

	/**
	 * 卸载操作 action 处理popmenu的点击事件
	 */
	private void actionUninstall(final Cell cell)
	{
		Tools.debugPrintln("卸载道具 -> actionUninstall");
		Tools.debugPrintln("celltype" + cell.m_column_type + "celltype.index" + cell.m_index);
		DWGameManager.getInstance().getSendMessage().sendOperaterCellMenuCommand(GameProtocol.操作菜单_卸下,
				(byte) cell.m_column_type, cell.m_index);
	}

	/**
	 * 装备
	 * 
	 * @param item
	 */
	private void actionEquip(Cell cell)
	{
		Tools.debugPrintln("装备道具 -> actionEquip");
		if (cell.m_item.m_type == ItemConstants.道具类型_消耗 && cell.m_item.m_subtype == ItemConstants.消耗_血蓝药)
			DWGameManager.getInstance().m_role.setShortcut(cell.m_item.m_key, Role.SHORTCUT_CONSUME);

		else if (cell.m_item.m_type == ItemConstants.道具类型_装备)
			DWGameManager.getInstance().getSendMessage().sendOperaterCellMenuCommand(GameProtocol.操作菜单_装备,
					(byte) cell.m_column_type, cell.m_index);
	}

	/**
	 * 销毁
	 * 
	 * @param item
	 */
	private void actionDestroy(final Cell cell)
	{
		final DWMessageBox tips = new DWMessageBox("提示", "你确定要销毁<mark c=" + getNameColor(cell.m_item.m_quality) + ">  "
				+ cell.m_item.m_name + "  </mark>吗?");
		tips.addListener(new DWListener() {
			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				DWControlsManager.getInstance().removeControl(tips);
				if (tips.getResult() == DWMessageBox.SOFTKEY_OK)
				{
					Tools.debugPrintln("销毁道具 -> actionDestroy");
					DWGameManager.getInstance().getSendMessage().sendOperaterCellMenuCommand(GameProtocol.操作菜单_删除,
							(byte) cell.m_column_type, cell.m_index);
				}
			}
		});
		DWControlsManager.getInstance().addControl(tips);
	}

	/**
	 * 移动
	 * 
	 * @param item
	 */
	private void actionMove(Cell fromCell, Cell toCell)
	{
		Tools.debugPrintln("移动道具 -> actionMove");
		DWGameManager.getInstance().getSendMessage().sendExchangeCellCommand((byte) fromCell.m_column_type,
				fromCell.m_index, (byte) toCell.m_column_type, toCell.m_index);
	}

	private long starTime = 0;
	private long lastTime = 0;
	private int dissmiss_time = 0;

	public void logic()
	{
		if (item_netFrame != null && DWControlsManager.getInstance().contains(item_netFrame))
		{
			if (starTime == 0)
			{
				starTime = System.currentTimeMillis() / 1000;
				lastTime = 0;
				dissmiss_time = 0;
			}
			lastTime = System.currentTimeMillis() / 1000;

			if (lastTime - starTime > dissmiss_time)
			{
				String msg;
				if (dissmiss_time % 6 == 0)
				{
					msg = "载入中";
				}
				else
				{
					msg = item_textbox.getText() + ".";
				}
				item_textbox.setText(msg);
				item_textbox.setNearAnchor(item_netFrame, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER);
				dissmiss_time++;
			}
		}
	}

	/**
	 * 查看道具详情
	 * */
	public void recvSeeItemInfos(DataInputStream dis) throws IOException
	{
		try {
			Item item = Item.recvItemFormServer(dis);
			if (item != null) {
				if (item.m_guid != -1) {
					DWGameManager.getInstance().putItemForCacheByGuid(item);
				}
				else {
					DWGameManager.getInstance().putItemForCacheByKey(item);
				}
			}
			showNetItemMessage(item);

			if (DWControlsManager.getInstance().contains(UIWindows.getInstance().m_fuben.m_lose_frame)) {
				if (UIWindows.getInstance().m_fuben.m_lose_frame != null) {
					for (int i = 0; i < UIWindows.getInstance().m_fuben.size1 + UIWindows.getInstance().m_fuben.size2
							+ UIWindows.getInstance().m_fuben.size3; i++) {
						UIWindows.getInstance().m_fuben.m_ima_bg[i]
								.setBackground(UIWindows.getInstance().m_fuben.m_cr_xxbj, false);
					}
				}
			}
			if (DWControlsManager.getInstance().contains(UIWindows.getInstance().m_tower.m_lose_frame))
			{
				if (UIWindows.getInstance().m_tower.m_lose_frame != null) {
					for (int i = 0; i < UIWindows.getInstance().m_tower.m_tower_list.get(0).m_itemsize; i++) {
						UIWindows.getInstance().m_tower.m_ima_bg[i]
								.setBackground(UIWindows.getInstance().m_tower.m_cr_xxxbj, false);
						UIWindows.getInstance().m_tower.m_grid[i].setSelect(false);
					}
				}
			}
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

	public DWGrid getGrid(String key)
	{
		DWGrid grid = null;

		if (key != null && !("".equals(key)))
		{
			int index = m_bag_tabcontrol.getPageIndex();
			CopyOnWriteArrayList<DWGrid> grids = m_bag_grids[index].m_grids;

			for (int i = 0; i < grids.size(); i++) {
				if (grids.get(i).m_data != null)
				{
					Cell cell = (Cell) grids.get(i).m_data;

					if (cell.m_item != null && cell.m_item.m_key.equals(key))
					{
						grid = grids.get(i);
						break;
					}
				}
				else
					break;
			}
		}

		return grid;
	}

	public DWControl getUser()
	{
		DWButton button = null;

		if (bag_item_Frame != null && DWControlsManager.getInstance().contains(bag_item_Frame))
		{
			if (m_item_button != null && buttonMenu != null && buttonMenu[m_item_button.length - 1].equals(USE))
			{
				button = m_item_button[m_item_button.length - 1];
			}
		}
		return button;
	}

	public DWButton getEquip()
	{
		DWButton button = null;

		if (bag_item_Frame != null && DWControlsManager.getInstance().contains(bag_item_Frame))
		{
			if (m_item_button != null && buttonMenu != null)
			{
				for (int i = 0; i < buttonMenu.length; i++)
				{
					if (buttonMenu[i].equals(EQUIP))
					{
						button = m_item_button[i];
						break;
					}
				}
			}
		}
		return button;
	}

	public void recvOpenPanel(DataInputStream dis)
	{
		try {
			byte state = dis.readByte();

			if (state == 0)
			{
				String msg = dis.readUTF();
				final DWMessageBox msgbox = new DWMessageBox("提示", msg);
				msgbox.addListener(new DWListener() {
					public void OnDoubleClick()
					{
					}

					public void OnClick()
					{
						DWControlsManager.getInstance().removeControl(msgbox);

						if (msgbox.getResult() == DWMessageBox.SOFTKEY_OK)
						{
							DWGameManager.getInstance().getSendMessage().sendOpenVipShop((byte) 1,
									(byte) 0);
						}
					}
				});
				DWControlsManager.getInstance().addControl(msgbox);
			}
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

	public byte m_shizhuang_state = 0;//1为显示普通装备 0为时装

	/**
	 * 时装外显display1102
	 * @param dis
	 */
	public void recvOpenshizhuang_display(DataInputStream dis)
	{
		Tools.debugPrintln("接收装备外显" + GameProtocol.SC_SHIZHUANG_DISPLAY);
		try {
			byte state = dis.readByte();
			m_shizhuang_state = state;
			Tools.debugPrintln("外显" + m_shizhuang_state);
//			if(m_bag_shizhuangButton!=null){
//				if(state == 0){
//					m_bag_shizhuangButton.setName("装备");
//				}else{
//					m_bag_shizhuangButton.setName("时装");
//				}
//			}
			if (m_shizhuang != null) {
				if (m_shizhuang_state == 0) {
					m_shizhuang.setImage(m_shizhuang2);
				}
				else {
					m_shizhuang.setImage(m_shizhuang1);
				}
			}

		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

	/**
	 * 更新时装外显状态1104
	 * @param dis
	 */
	public void recvOpenshizhuang_response(DataInputStream dis)
	{
		Tools.debugPrintln("设置装备外显" + GameProtocol.SC_SHIZHUANG_STATE);
		try {
			byte state = dis.readByte();
			if (state == 0) {
				DWGameManager.getInstance().addSystemInfo(
						DWGameManager.SYSTEM_INFO_TYPE_B, "设置失败！");
				if (m_shizhuang_state == 0) {
					sendshizhuang((byte) 1);
				}
				else {
					sendshizhuang((byte) 0);
				}
			}
			else {
				DWGameManager.getInstance().addSystemInfo(
						DWGameManager.SYSTEM_INFO_TYPE_B, "设置成功！");
//				if(m_shizhuang_state == 0){
//					m_shizhuang_state = 1;
//					m_bag_shizhuangButton.setName("时装");
//				}else{
//					m_shizhuang_state = 0;
//					m_bag_shizhuangButton.setName("装备");
//				}
				if (m_shizhuang != null) {
					if (m_shizhuang_state == 0) {
						m_shizhuang_state = 1;
						m_shizhuang.setImage(m_shizhuang2);
					}
					else {
						m_shizhuang_state = 0;
						m_shizhuang.setImage(m_shizhuang1);
					}
				}
			}
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

	public void sendshizhuang(byte state)
	{
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		try {
			dos.writeByte(state);
			DWGameManager
					.getInstance()
					.getConnection()
					.sendMessage(0, GameProtocol.CS_SHIZHUANG_STATE,
							baos.toByteArray());
			Tools.debugPrintln("发送指令" + GameProtocol.CS_SHIZHUANG_STATE + "!");
			dos.close();
			dos = null;
			baos.close();
			baos = null;
		}
		catch (IOException e) {
			Tools.debug(e);
		}
	}

}
